package com.yunji.app.coupon;

import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.utils.ConvertDataUtil;
import com.yunji.coupon.bo.CouponBO;
import com.yunji.coupon.business.CouponBusiness;
import com.yunji.coupon.doamin.CouponDO;
import com.yunji.coupon.dto.CouponListDTO;
import com.yunji.coupon.dto.SystemReceiveDTO;
import com.yunji.coupon.vo.CouponVO;
import com.yunji.coupon.vo.UseCouponVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2024/10/31 18:10
 * @description : TODO
 **/
@Api(tags = "优惠券")
@RestController
@RequestMapping("/gateway/coupon")
@Validated
public class AppCouponController {
    @Resource
    private CouponBusiness couponBusiness;

    @ApiOperation("系统弹窗自动领取优惠券")
    @PostMapping("/systemReceiveList")
    public Response<List<CouponVO>> systemReceiveList(@Validated @RequestBody SystemReceiveDTO dto) {
        List<CouponVO> voList = couponBusiness.systemAutoReceive(dto);
        return ResponseHelper.success(voList).withDataIds(ConvertDataUtil.list2FieldList(voList, CouponVO::getCouponId));
    }

    @ApiOperation("下单时未使用优惠券列表(过滤网点并让用户选择优惠券)")
    @GetMapping("/unUsedSelectList/{siteId}")
    public Response<UseCouponVO> unUsedSelectList(@PathVariable Long siteId) {
        return ResponseHelper.success(couponBusiness.unUsedSelectList(siteId));
    }

    @ApiOperation("下单后未领取优惠券列表")
    @GetMapping("/unReceiveList/{orderId}")
    public Response<List<CouponVO>> unReceiveList(@PathVariable @NotNull Long orderId) {
        return ResponseHelper.success(couponBusiness.getUnReceiveList(orderId));
    }

    @ApiOperation("下单后根据模板ID领取优惠券")
    @PutMapping("/create/{couponTmplId}")
    public Response<Object> createByTmplId(@PathVariable Long couponTmplId) {
        CouponBO couponBO = couponBusiness.receiveAvailableCoupon(couponTmplId);
        CouponDO domain = couponBO.getDomain();
        Assert.isTrue(domain != null, "领取失败");
        return ResponseHelper.success().withDataId(domain.getCouponId());
    }

    @ApiOperation("我的优惠券")
    @GetMapping("/mineCouponList")
    public Response<List<CouponVO>> mineCouponList() {
        return ResponseHelper.success(couponBusiness.userCouponList(new CouponListDTO()));
    }
}
